import Login from './views/public/Login.vue'
import NotFound from './views/public/404.vue'
import Home from './views/public/Home.vue'
import stuScore from './views/student/score.vue'
import moneystate from './views/student/moneystate.vue'  //学生学籍信息有改动 ->马松

import schange from './views/student/schange.vue'//异动申请页面
import plan from './views/student/plan.vue'
import thesis from './views/student/thesis.vue'    //毕业论文界面 - 周长磊

import stu_message from './views/student/stu_message.vue' //学籍信息->马松

import class_index from './views/student/class_index.vue' //课程界面
import stu_course from './views/student/stu_course.vue' //学习课程界面
import teach_info from './views/student/teacherInfo.vue' //教师信息界面
import notice from './views/student/notice.vue' //教学公告界面
import studyResource from './views/student/studyResource.vue' //资源下载界面
import courseInfo from './views/student/courseInfo.vue' //课程详细信息
import play_video from './views/student/play_video.vue' //播放视频页面
import history_class from './views/student/history_class.vue' //历史课程
import sectionTest from './views/student/sectionTest.vue' //章节测试
import sectionTestPaper from './views/student/sectionTestPaper.vue' //试卷
import courseTest from './views/student/courseTest.vue' //课程测试

import Register from './views/register/register.vue'//报名
import Reg_notice from './views/register/reg_notice.vue' //报名须知
import Reg_information from './views/register/reg_information.vue' //报名信息
import Reg_query from './views/register/reg_query.vue' //查询
import enter_test from './views/register/enter_test.vue'//入学考试

import Exam from './views/student/exam.vue'  //考试相关信息
import Paper from './views/student/exam_paper.vue' //考试详情
import course_notice from './views/student/course_notice.vue' //曹朋 教学通知
import print from './views/student/print.vue'//郭伟 打印准考证

import VueRouter from 'vue-router';
import Vue from 'vue';

Vue.use(VueRouter);

let routes = [{
    path: '/login',
    component: Login,
    name: '',
    hidden: true,
    meta: {
        requiresAuth: 0
    }
},
    {
        path: '/das',
        component: Home,
        name: '',
        hidden: true,
        meta: {
            requiresAuth: 0
        }
    },
    {
        path: '/home',
        component: Home,
        name: '学生主页',
        hidden: true,
        meta: {
            requiresAuth: 0
        }
    },

    //******************************************
    {
        path: '/home',
        component: Home,
        name: '报名主页',
        iconCls: "",
        hidden: false,
        leaf: true,
        children: [
            {path: '/reg_notice', component: Reg_notice, name: '报名须知'}
        ],
        meta: {
            requiresAuth: 5
        }
    },
    {
        path: '/home',
        component: Home,
        name: '报名主页',
        iconCls: "",
        leaf: true,
        hidden: false,
        children: [
            {path: '/reg_information', component: Reg_information, name: '报名信息'},
        ],
        meta: {
            requiresAuth: 5
        }
    },
    {
        path: '/home',
        component: Home,
        name: '报名主页',
        iconCls: "",
        leaf: true,
        hidden: false,
        children: [
            {path: '/enter_test', component: enter_test, name: '入学考试'}
        ],
        meta: {
            requiresAuth: 5
        }
    },

    {
        path: '/home',
        component: Home,
        name: '报名主页',
        iconCls: "",
        leaf: true,
        hidden: false,
        children: [
            {path: '/reg_query', component: Reg_query, name: '查询'}
        ],
        meta: {
            requiresAuth: 5
        }

    },

    //**************************************


    {
        path: '/404',
        component: NotFound,
        name: '',
        hidden: true,
        meta: {
            requiresAuth: 0
        }
    },
    {
        path: '/stu_index',
        component: Home,
        name: '学生主页',
        iconCls: 'el-icon-menu',
        leaf: true,
        children: [


            //{ path: '/stu_index', component: stu_index, name: '学生主页' },
            {path: '/class_index', component: class_index, name: '学生主页', hidden: true},
            {path: '/stu_course', component: stu_course, name: '学习课程', hidden: true},
            {path: '/teach_info', component: teach_info, name: '教师信息', hidden: true},
            {path: '/course_notice', component: course_notice, name: '教学公告', hidden: true},
            {path: '/studyResource', component: studyResource, name: '资源下载', hidden: true},
            {path: '/courseInfo', component: courseInfo, name: '课程详细信息', hidden: true},
            {path: '/play_video', component: play_video, name: '视频播放', hidden: true},

            {path: '/history_class', component: history_class, name: '历史课程', hidden: true},
            {path: '/sectionTest', component: sectionTest, name: '章节测试'},
            {path: '/sectionTestPaper', component: sectionTestPaper, name: '试卷'},
            {path: '/courseTest', component: courseTest, name: '选择课程'},

        ],
        meta: {
            requiresAuth: 6
        }
    },
    {
        path: '/sys1',
        component: Home,
        name: '系统公告',
        iconCls: 'el-icon-menu',
        leaf: true,
        children: [
            {path: '/my_3', component: notice, name: '系统公告'},
        ],
        meta: {
            requiresAuth: 6
        }
    },
    {
        path: '/',
        component: Home,
        name: '学费缴纳状态',
        iconCls: 'el-icon-menu',
        leaf: true,
        children: [
            {path: '/money', component: moneystate, name: '学费缴纳状态'},
        ],
        meta: {
            requiresAuth: 6
        }
    },

    /**
     * 学生端学籍信息   马松
     */
    {
        path: '/stu_message',
        component: Home,
        name: '学籍信息',
        iconCls: 'el-icon-menu',
        leaf: true,
        children: [
            {path: '/stu_message', component: stu_message, name: '学籍信息'},
        ],
        meta: {
            requiresAuth: 6
        }
    },


    {
        path: '/jihua',
        component: Home,
        name: '教学计划',
        iconCls: 'el-icon-menu',
        leaf: true,
        children: [
            {path: '/plan', component: plan, name: '教学计划'},
        ],
        meta: {
            requiresAuth: 6
        }
    },

    /**
     * 学生端学籍信息   马松
     */

    {
        path: '/score',
        component: Home,
        name: '学生成绩',
        iconCls: 'el-icon-menu',
        leaf: true,
        children: [
            {path: '/score', component: stuScore, name: '学生成绩'},
        ],
        meta: {
            requiresAuth: 6
        }
    },
    {
        path: '/',
        component: Home,
        name: '考试安排',
        iconCls: 'el-icon-menu',
        leaf: true,
        children: [
            {path: '/kaoshi', component: Exam, name: '考试安排'},
            {path: '/Paper', component: Paper, name: '考试'}
        ],
        meta: {
            requiresAuth: 6
        }
    },
    {
        path: '/',
        component: Home,
        name: '打印准考证',
        iconCls: 'el-icon-menu',
        leaf: true,
        children: [
            {path: '/print', component: print, name: '打印准考证'}
        ],
        meta: {
            requiresAuth: 6
        }
    },
    {
        path: '/',
        component: Home,
        name: '毕业论文',
        iconCls: 'el-icon-menu',
        leaf: true,
        children: [
            // { path: '/lunwen', name: '毕业论文' }
            {path: '/thesis', component: thesis, name: '毕业论文'}
        ],
        meta: {
            requiresAuth: 6
        }
    },
    {
        path: '/',
        component: Home,
        name: '异动申请',
        iconCls: 'el-icon-menu',
        leaf: true,
        children: [
            {path: '/schange', component: schange, name: '异动申请'}
        ],
        meta: {
            requiresAuth: 6
        }
    },

    {
        path: '*',
        hidden: true,
        redirect: {path: '/404'},
        meta: {
            requiresAuth: 0
        }
    }
];

const router = new VueRouter({
    routes,
    mode: 'history' //去掉难看的#号
});

const ignore = ['/login', '/register', '/reg_notice', '/reg_information', '/reg_query','/enter_test'];//不需要登陆权限的路由

router.beforeEach((to, from, next) => {
    let stu = JSON.parse(sessionStorage.getItem('stu'));
    // if (to.path == '/login' && stu != null) {
    //     if (stu.rememberMe == true) {
    //         next({path: '/home'});
    //     } else {
    //         sessionStorage.removeItem("stu");
    //         next({path: '/login'});
    //     }
    // }
    // if (stu == null && to.path != '/login') {
    //     next({path: '/login'})
    // } else {
    //     next()
    // }
    // next()


    if (!stu) {//非登陆用户
        if (ignore.indexOf(to.path) >=0||to.path=="/home") {
            filter(routes, 5);
            if (to.path=="/home"){
                next({path:"/reg_notice"});
            }
            else{
                next();
            }
        }
        else {
            next({path: "/login"});
        }
    }
    else { // 登陆用户
        if (to.path == "/") {
            sessionStorage.removeItem("stu");
            next({path: "/login"});
        }
        else {
            filter(routes,6);
            if (to.path=="/home"){
                next({path:"/class_index"});
            }else {
                next();
            }

        }
    }

});

function filter(filerounte, role) {
    for (var index in filerounte) {
        var myrouterole = filerounte[index].meta.requiresAuth
        if (myrouterole != role) {
            filerounte[index].hidden = true;
        } else if (myrouterole == role) {
            console.log(index);
            filerounte[index].hidden = false;
        }
    }
    return filerounte;
}

export default router;